Python – How to deal with UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape – iTecNote 您所在的位置:网站首页 python sparse矩阵 Python – How to deal with UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape – iTecNote

Python – How to deal with UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape – iTecNote

2023-03-29 04:30| 来源: 网络整理| 查看: 265

The SettingWithCopyWarning was created to flag potentially confusing "chained" assignments, such as the following, which does not always work as expected, particularly when the first selection returns a copy. [see GH5390 and GH5597 for background discussion.]

df[df['A'] > 2]['B'] = new_val # new_val not set in df

The warning offers a suggestion to rewrite as follows:

df.loc[df['A'] > 2, 'B'] = new_val

However, this doesn't fit your usage, which is equivalent to:

df = df[df['A'] > 2] df['B'] = new_val

While it's clear that you don't care about writes making it back to the original frame (since you are overwriting the reference to it), unfortunately this pattern cannot be differentiated from the first chained assignment example. Hence the (false positive) warning. The potential for false positives is addressed in the docs on indexing, if you'd like to read further. You can safely disable this new warning with the following assignment.

import pandas as pd pd.options.mode.chained_assignment = None # default='warn' Other Resources pandas User Guide: Indexing and selecting data Python Data Science Handbook: Data Indexing and Selection Real Python: SettingWithCopyWarning in Pandas: Views vs Copies Dataquest: SettingwithCopyWarning: How to Fix This Warning in Pandas Towards Data Science: Explaining the SettingWithCopyWarning in pandas


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有